package com.minelx.scriptbuilder.parse.parser;

import com.minelx.sequence.Collector;
import com.minelx.sequence.CompositeMatcher;
import com.minelx.sequence.IStringScanner;
import com.minelx.sequence.ReturnMatcher;
import com.minelx.scriptbuilder.script.ScriptContext;
import com.minelx.scriptbuilder.script.operation.CompositeOperation;
import com.minelx.scriptbuilder.script.operation.IOperation;

import static com.minelx.sequence.Collector.toList;

public class StatementsParser implements ReturnMatcher<IOperation, IStringScanner> {

	private final CompositeMatcher<IOperation, IStringScanner> parsers;

	public StatementsParser(ScriptContext context) {
		parsers = new CompositeMatcher<IOperation, IStringScanner>()
				.add(new ReturnParser(context))
				.add(new AssignParser(context))
				.add(new FunctionInvocationParser(context));
	}

	@Override
	public boolean isMatch(IStringScanner scanner) {
		return parsers.isMatch(scanner);
	}

	@Override
	public IOperation onMatched(IStringScanner scanner) {
		return new CompositeOperation().addAll(scanner.sequence(parsers).collect(toList()));
	}
}
